
package krush.rezepte;

import java.util.ArrayList;

import strukturen.Kategorie;
import strukturen.Rezept;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;


public class KategorieSuche extends ExpandableListActivity
{
	ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
    }
    
    @Override
    public void onResume()
    {
	super.onResume();
	init();
    }
    
    private void init()
    {
    	mAdapter = new MyExpandableListAdapter(this);
        setListAdapter(mAdapter);
        registerForContextMenu(getExpandableListView());
        this.getExpandableListView().setBackgroundColor(Color.WHITE);
        this.getExpandableListView().setDrawingCacheBackgroundColor(Color.WHITE);
        this.getExpandableListView().setCacheColorHint(Color.WHITE);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
    {
    	
        menu.setHeaderTitle("Auswahl");
        menu.add(0, 0, 0, "Rezept löschen");
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
    {
    	long r_id = mAdapter.getChildId(groupPosition, childPosition);
    	Intent intent = new Intent(this, RezeptView.class);
    	intent.putExtra("rid", r_id);
		startActivity(intent);
    	return true;
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) 
    {
    	
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
            Long id = mAdapter.getChildId(groupPos, childPos);
            
            Boolean delSucc = (new DBHelper(this)).deleteRezept(id);
            if (delSucc)
            	Toast.makeText(this, "Rezept gelöscht!", Toast.LENGTH_SHORT).show();
            else
            	Toast.makeText(this, "Konnte Rezept nicht löschen!", Toast.LENGTH_SHORT).show();
            
            init();
            
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) 
        {           
            
            Toast.makeText(this, "Kategorien können nicht gelöscht werden", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }
    
       
	 public class MyExpandableListAdapter extends android.widget.BaseExpandableListAdapter 
	 {	       
	        private String[] groups;
	        private String[][][] children;
	        ArrayList<Rezept> rl;
		 
		 public MyExpandableListAdapter(Context c)
		 {			 
			 ArrayList<Kategorie> kList;
		     DBHelper db = new DBHelper(c);        
		     kList = db.getKategorien();
			 rl = db.getRezepte();
			 
			 
			 groups = new String[kList.size()];
			 children = new String[kList.size()][][];
			 
			 for (int i = 0; i < kList.size(); i++)
			 {				 
				 groups[i] = kList.get(i).getName();
				 
				 //Zaehlen der Rezepte pro Kategorie
				 int z = 0;
				 ArrayList<Rezept> temp = new ArrayList<Rezept>();
				 for (int j = 0; j < rl.size(); j++)
				 {
					 int x = rl.get(j).getKat_id();
					 int y = kList.get(i).getId();
					if (x == y)
					{
						z++;
						temp.add(rl.get(j));
					}
				 }
				 
				 children[i] = new String[z][2];
					for (int k = 0; k < z; k++)
					{
						children[i][k][0] = temp.get(k).getName() + "\n(" + db.getSuchbegriffeForRezeptnummer(temp.get(k).getId()) + ")" ;
						children[i][k][1] = String.valueOf(temp.get(k).getId());
					}				 
			 }
	        rl = db.getRezepte();
		 }

	        public Object getChild(int groupPosition, int childPosition) 
	        {	        	
	            return children[groupPosition][childPosition][0];	        	
	        }

	        public long getChildId(int groupPosition, int childPosition) {

	        	String x = children[groupPosition][childPosition][1];
	        	return Long.decode(x);
	        }

	        public int getChildrenCount(int groupPosition) {
	            return children[groupPosition].length;
	        }

	        public TextView getGenericView() 
	        {
	            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

	            TextView textView = new TextView(KategorieSuche.this);
	            textView.setLayoutParams(lp);	            
	            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);	            
	            textView.setPadding(60, 0, 0, 0);
	            textView.setTextSize(20);
	            textView.setTextColor(Color.parseColor("#222222"));
	            return textView;
	        }

	        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
	            LinearLayout l = new LinearLayout(KategorieSuche.this);
	            
	            TextView textView = getGenericView();
	            textView.setText(getChild(groupPosition, childPosition).toString());
	            textView.setTextSize(15);
	            textView.setPadding(80, 0, 0, 0);
	            textView.setTextColor(Color.parseColor("#666666"));
	            l.addView(textView);	            
	        	
	            return l;
	        }

	        public Object getGroup(int groupPosition) {
	            return groups[groupPosition];
	        }

	        public int getGroupCount() {
	            return groups.length;
	        }

	        public long getGroupId(int groupPosition) {
	            return groupPosition;
	        }

	        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
	                ViewGroup parent) {
	            TextView textView = getGenericView();
	            textView.setText(getGroup(groupPosition).toString());
	            return textView;
	        }

	        public boolean isChildSelectable(int groupPosition, int childPosition) {
	            return true;
	        }

	        public boolean hasStableIds() {
	            return true;
	        }

	    }
}

